//
//  ChooseInterestController.m
//  OnionHome
//
//  Created by fengjie on 2019/10/2.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "ChooseInterestController.h"
#import "OnionCertificationTwoController.h"

@interface ChooseInterestController ()<UICollectionViewDelegate,UICollectionViewDataSource>
/**<#属性#>*/
@property (nonatomic,strong)UICollectionView*listView;
/**<#属性#>*/
@property (nonatomic,strong) UICollectionViewFlowLayout * layout;
/**数据源*/
@property (nonatomic,strong)NSMutableArray*dataArray;

@end

@implementation ChooseInterestController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择兴趣领域";
    [self createUI];
//    self.view.backgroundColor=UIColorFromRGB(0xF5F7F5);

}
-(void)createUI{
    [self.view addSubview:self.listView];
    UIButton * bottomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [bottomBtn setBackgroundImage:IMAGE_NAMED(@"guanzhu_bg") forState:UIControlStateNormal];
    [bottomBtn setTitle:@"完成" forState:UIControlStateNormal];
    kWeakSelf(weakSelf);
    [bottomBtn addActionHandler:^{
        [weakSelf.navigationController pushViewController:[OnionCertificationTwoController new] animated:YES];
    }];
    [self.view addSubview:bottomBtn];
    [bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];

}
#pragma mark ---UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ChooseInterestCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(ChooseInterestCell.class) forIndexPath:indexPath];
    cell.titleName = self.dataArray[indexPath.row];
    return cell;
}
#pragma mark  定义每个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = (KScreenWidth - 32 - 40*2) /3;
    return CGSizeMake(width, 45);
}

// 列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 40;
}
//// 行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 20;
}

#pragma mark ---懒加载
-(UICollectionView *)listView{
    if (!_listView) {
        self.layout= [[UICollectionViewFlowLayout alloc] init];
        _listView = [[UICollectionView alloc] initWithFrame:CGRectMake(16, 20, KScreenWidth - 32, KScreenHeight-K_TopHeight-50 - 20) collectionViewLayout:self.layout];
        _listView.backgroundColor = KWhiteColor;
        _listView.delegate=self;
        _listView.dataSource = self;
        _listView.scrollEnabled = YES;
        [_listView registerClass:[ChooseInterestCell class] forCellWithReuseIdentifier:NSStringFromClass(ChooseInterestCell.class)];
        
    }
    return _listView;
}
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        NSArray * array = @[@"美食资讯",@"区域美食",@"美食视频",@"做菜达人",@"美食能人",@"美食能人",@"大胃王"];
        _dataArray =[[NSMutableArray alloc] init];
        [_dataArray addObjectsFromArray:array];
    }
    return _dataArray;
}

@end


@interface ChooseInterestCell ()
/**中间文字*/
@property (nonatomic,strong)UILabel*titlelabel;

@end


@implementation ChooseInterestCell

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        ViewBorderRadius(self, 3, 1,  UIColorFromRGB(0xF5F7F5));
        
        UIView * selectView = UIView.new;
        selectView.backgroundColor = UIColorFromRGB(0xECFBF0);
        self.selectedBackgroundView =selectView;
        
        
        [self addSubview:self.titlelabel];
        
        [self.titlelabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(0);
        }];
        
    }
    return self;
}
-(void)setTitleName:(NSString *)titleName{
    _titleName = titleName;
    self.titlelabel.text = titleName;
}
-(void)setSelected:(BOOL)selected{
    if (selected) {
        self.titlelabel.textColor = UIColorFromRGB(0x009966);
    }else{
        self.titlelabel.textColor = KBlackColor;
    }
}
-(UILabel*)titlelabel{
    if(!_titlelabel){
        _titlelabel=[UILabel new];
        _titlelabel.textColor = UIColorFromRGB(0x273323);
        _titlelabel.font = SYSTEMFONT(18);
        _titlelabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titlelabel;
}


@end
